/**
 * Created by 11450 on 2017/2/11.
 */

angular.module('NatureCloud').controller('BuildManualUpdateController', ["$scope", "$http", "$uibModalInstance", "$cookies", "buildUpdate", function ($scope, $http, $uibModalInstance, $cookies, buildUpdate) {
    $scope.operation = "更新构建";
    $scope.disabled = true;
    $scope.isRepoListShow = false;
    $scope.isRepoInfoShow = true;
    var buildInfo = $scope.updateBuildInfo;
    $scope.id = buildInfo.id;
    $scope.gitPath = buildInfo.gitPath;
    $scope.user = buildInfo.user;
    $scope.buildContext = buildInfo.buildContext;
    $scope.repoName = buildInfo.repoName;

    $scope.isCI = buildInfo.isCI;
    $scope.username = buildInfo.username;
    $scope.password = buildInfo.password;

    $scope.buildName = buildInfo.id;

    $scope.type = buildInfo.type;

    $scope.ok = function () {
        if (buildInfo.gitPath == $scope.gitPath && buildInfo.buildContext == $scope.buildContext && buildInfo.type ==
            $scope.type && $scope.username == buildInfo.username && $scope.password == buildInfo.password) {
            alertify.success('nothing to change');
        } else {
            var user = $cookies.get("username");
            $scope.updateBuildInfo.buildContext = ($scope.buildContext === undefined) ? '' : $scope.buildContext;
            $scope.updateBuildInfo.repoName = buildInfo.id;
            $scope.updateBuildInfo.type = $scope.type;
            $scope.updateBuildInfo.gitPath = $scope.gitPath;
            $scope.updateBuildInfo.isCI = ($scope.isCI === undefined) ? buildInfo.isCI : $scope.isCI;
            $scope.updateBuildInfo.username = $scope.username;
            $scope.updateBuildInfo.password = $scope.password;


            buildUpdate(angular.toJson($scope.updateBuildInfo, true)).then(function (r) {
                var data = r.data.data;
                if (data.n === 1 && data.nModified === 1 && data.ok === 1) {
                    alertify.success("build update succeed");
                    $uibModalInstance.close(data.jobInfo);
                } else {
                    alertify.error("数据库更新失败");
                    $uibModalInstance.close();
                }
            }, function (r) {

                alertify.error("build update failed");

            });
        };
        $uibModalInstance.close();
    }
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
}]);